import os
import shutil
import time
from xml.dom.minidom import parseString
import cv2 as cv
import re
# 注意pc下的C:/Users/冯攀/Documents/雷电模拟器/Pictures/和安卓上面的/sdcard/Pictures/文件夹是相通的
class DnPlayer(object):

    def __init__(self, info: list):
        super(DnPlayer, self).__init__()
        # 索引，标题，顶层窗口句柄，绑定窗口句柄，是否进入android，进程PID，VBox进程PID
        self.index = int(info[0])
        self.name = info[1]
        self.top_win_handler = int(info[2])
        self.bind_win_handler = int(info[3])
        self.is_in_android = True if int(info[4]) == 1 else False
        self.pid = int(info[5])
        self.vbox_pid = int(info[6])

    def is_running(self) -> bool:
        return self.is_in_android

    def __str__(self):
        index = self.index
        name = self.name
        r = str(self.is_in_android)
        twh = self.top_win_handler
        bwh = self.bind_win_handler
        pid = self.pid
        vpid = self.vbox_pid
        return "\nindex:%d name:%s top:%08X bind:%08X running:%s pid:%d vbox_pid:%d\n" % (
            index, name, twh, bwh, r, pid, vpid)

    def __repr__(self):
        index = self.index
        name = self.name
        r = str(self.is_in_android)
        twh = self.top_win_handler
        bwh = self.bind_win_handler
        pid = self.pid
        vpid = self.vbox_pid
        return "\nindex:%d name:%s top:%08X bind:%08X running:%s pid:%d vbox_pid:%d\n" % (
            index, name, twh, bwh, r, pid, vpid)


class NodeInfo(object):
    def __init__(self, text: str = ""):
        super(NodeInfo, self).__init__()
        self.info = []
        if len(text) == 0:
            return
        self.__xml = parseString(text)
        nodes = self.__xml.getElementsByTagName('node')
        for node in nodes:
            node_item = dict()
            attrs = node.attributes
            text = node.getAttribute('text')
            for attr in attrs.keys():
                value = node.getAttribute(attr)
                if(attr == 'bounds'):
                    matches = re.findall(r'\[(.*?)\]', value)
                    value = [list(map(int, match.split(','))) for match in matches]
                node_item[attr] = value
            self.info.append(node_item)
    def get_nodes_by_name(self, name: str):
        result_nodes = []
        for node in self.info:
            if node['text'] == name:
                result_nodes.append(node)
        return result_nodes

    def get_node_by_name(self, name: str):
        for node in self.info:
            if node['text'] == name:
                return node
        return None